/*
 * @Description:
 * @Version: 0.1
 * @Autor: Civet
 * @Date: 2019-08-02 16:04:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2019-10-25 10:29:55
 */

import Vue from "vue";
import Router from "vue-router";
// import templateFile from '@/components/templateFile' // 模板页
// import test from '@/views/test'

import {
  Message
} from "element-ui";

import login from "@/views/login/loginLogic"; // 登录
import frame from "@/views/frame/frameView";
import frameWork from "@/views/frameWork";

import homeLogic from "@/views/home/homeLogic"; // 主页
//  外接资源管理
import {
  ExternalManagement
} from "./ExternalManagement";
import {
  ServiceResources
} from "./ServiceResources";
import {
  MoreUse
} from "./MoreUse";
import {
  InstitutionalFile
} from "./InstitutionalFile";
import notFound from "@/views/404.vue";

let session = window.sessionStorage;

Vue.use(Router);
// 不知道是不是Element-ui 升级后就会导致点击同样的路由会报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const frameChildren = [
    //  首页
    {
      name: "home",
      path: "home",
      component: homeLogic
    },
    //  动态表单初级页面
    {
      name: 'DynamicForm',
      path: 'DynamicForm',
      meta: {
        requiresAuth: true // 说明需要鉴权
      },
      component: resolve => require(["@/views/DynamicForm/check"], resolve),
    }
  ].concat(ExternalManagement)
  .concat(ServiceResources)
  .concat(MoreUse)
  .concat(InstitutionalFile)
const router = new Router({
  // mode: 'history',
  routes: [
    //  登录页
    {
      path: "/",
      name: "login",
      component: login
    },
    //  首页
    {
      path: "/frame",
      component: frame,
      redirect: "/frame/home",
      meta: {
        requiresAuth: true // 说明需要鉴权
      },
      children: frameChildren
    },
    // //  外接资源管理
    // {
    //   path: 'ExternalManagement',
    //   name: "ExternalManagement",
    //   component: resolve => require(["@/views/ExternalManagement/MetadataRegistration"], resolve),
    //   redirect: '/frame/ExternalManagement/MetadataRegistration',
    //   children: ExternalManagementChildren
    // },
    // {
    //   path: 'register',
    //   name: "register",
    //   component: resolve => require(["@/views/ExternalManagement/MetadataRegistration"], resolve),
    //   redirect: '/frame/ExternalManagement/MetadataRegistration',
    //   children: ExternalManagementChildren
    // },
    //  首页去除菜单的子页面
    {
      path: "/frameWork",
      component: frameWork,
      redirect: "/frameWork/home",
      meta: {
        requiresAuth: true // 说明需要鉴权
      },
      children: frameChildren
    },
    //  报错404页面
    {
      path: "*",
      name: "noFound",
      component: notFound
    }
  ]
});

// 全局守卫  https://router.vuejs.org/zh-cn/advanced/navigation-guards.html
router.beforeEach((to, from, next) => {
  // 获得那些路由需要token验证
  // console.log("to", to);
  // console.log("from", from);
  // console.log("next", next);
  let userData = JSON.parse(session.getItem("userData"));
  if (to.path == "/" && userData) {
    next("/frame");
  } else if (to.matched.some(route => route.meta.requiresAuth)) {
    console.log("userData", userData);
    if (userData) {
      next();
    } else {
      // 没有登录的提示
      Message.error("您还没有登录");
      next("/");
    }
  } else {
    next(); // 确保一定要调用
  }
  // next();
});

export default router;
